home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / overview / moreisbetter / mib-libraries / moreappleevents / moreappleevents.cp next >
Encoding:
Text File  |  2000-06-23  |  4.3 KB  |  177 lines

  1. /*
  2.     File:        MoreAppleEvents.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by:    Pete Gontier
  7.  
  8.     Copyright:    Copyright (c) 1998 Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.  
  20.          <4>     2/15/99    PCG     add AEGetDescDataSize for non-Carbon clients
  21.          <3>     1/29/99    PCG     add AEGetDescData
  22.          <2>    11/11/98    PCG     fix header
  23.          <1>    11/10/98    PCG     first big re-org at behest of Quinn
  24.  
  25.     Old Change History (most recent first):
  26.  
  27.          <2>    10/11/98    Quinn   Convert "MorePrefix.h" to "MoreSetup.h".
  28.          <2>     6/16/98    PCG     CreateProcessTarget works with nil PSN
  29.          <1>     6/16/98    PCG     initial checkin
  30. */
  31.  
  32. #include "MoreSetup.h"
  33. #include "MoreAppleEvents.h"
  34. #include "MoreProcesses.h"
  35.  
  36. pascal OSErr SetReplyErrorNumber (OSErr errToReport, AppleEvent *reply)
  37. {
  38.     OSErr err = noErr;
  39.  
  40.     if (reply->dataHandle)
  41.     {
  42.         if (!MoreAssert (reply->descriptorType == typeAppleEvent))
  43.             err = paramErr;
  44.         else
  45.             err = AEPutParamPtr (reply,keyErrorNumber,typeShortInteger,&errToReport,sizeof(errToReport));
  46.     }
  47.  
  48.     return err;
  49. }
  50.  
  51. pascal OSErr SenderIsFinder (const AppleEvent *event, Boolean *isFinder)
  52. {
  53.     if (!MoreAssert (event && isFinder))                            return paramErr;
  54.     if (!MoreAssert (event->descriptorType == typeAppleEvent))        return paramErr;
  55.     if (!MoreAssert (event->dataHandle))                            return paramErr;
  56.  
  57.     OSErr                    err = noErr;
  58.     DescType                actualType;
  59.     ProcessSerialNumber        senderPSN;
  60.     Size                    actualSize;
  61.  
  62.     err = AEGetAttributePtr (event, keyAddressAttr, typeProcessSerialNumber, &actualType,
  63.         (Ptr) &senderPSN, sizeof (senderPSN), &actualSize);
  64.     if (MoreAssert (err == noErr))
  65.     {
  66.         if (!MoreAssert (actualType == typeProcessSerialNumber))
  67.             err = paramErr;
  68.         else if (!MoreAssert (actualSize == sizeof (senderPSN)))
  69.             err = paramErr;
  70.         else
  71.         {
  72.             ProcessInfoRec processInfo;
  73.  
  74.             if (!(err = GetSomeProcessInfo (&senderPSN,&processInfo)))
  75.             {
  76.                 *isFinder = (processInfo.processSignature == 'MACS' && processInfo.processType == 'FNDR');
  77.             }
  78.         }        
  79.     }
  80.  
  81.     return err;
  82. }
  83.  
  84. pascal OSErr CreateProcessTarget (ProcessSerialNumber *psn, AEDesc *target)
  85. {
  86.     ProcessSerialNumber self;
  87.  
  88.     if (!psn)
  89.     {
  90.         psn = &self;
  91.  
  92.         self.lowLongOfPSN        = kNoProcess;
  93.         self.highLongOfPSN        = kCurrentProcess;
  94.     }
  95.  
  96.     return AECreateDesc (typeProcessSerialNumber,psn,sizeof(*psn),target);
  97. }
  98.  
  99. pascal OSErr CreateSimpleAppleEvent
  100.     (AEEventClass eventClass, AEEventID eventID, ProcessSerialNumber *psn, AppleEvent *result)
  101. {
  102.     OSErr err = noErr;
  103.  
  104.     AEDesc target;
  105.  
  106.     if (!(err = CreateProcessTarget (psn,&target)))
  107.     {
  108.         OSErr err2;
  109.  
  110.         err = AECreateAppleEvent (eventClass,eventID,&target,kAutoGenerateReturnID,kAnyTransactionID,result);
  111.  
  112.         err2 = AEDisposeDesc (&target);
  113.         if (!err) err = err2;
  114.     }
  115.  
  116.     return err;
  117. }
  118.  
  119. pascal OSErr SimplySendAppleEvent (const AppleEvent *appleEvent, AppleEvent *reply)
  120. {
  121.     OSErr err = noErr;
  122.  
  123.     AESendMode aeSendMode = kAEAlwaysInteract | kAECanSwitchLayer;
  124.  
  125.     if (reply)
  126.     {
  127.         aeSendMode |= kAEWaitReply;
  128.  
  129.         reply->descriptorType    = typeNull;
  130.         reply->dataHandle        = nil;
  131.     }
  132.  
  133.     err = AESend (appleEvent, reply, aeSendMode, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  134.  
  135.     return err;
  136. }
  137.  
  138. #if !TARGET_CARBON
  139.  
  140. pascal Size AEGetDescDataSize (const AEDesc *desc)
  141. {
  142.     Size result = 0;
  143.  
  144.     if (MoreAssert (desc) && desc->dataHandle)
  145.     {
  146.         result = GetHandleSize (desc->dataHandle);
  147.  
  148.         if (!MoreAssert (noErr == MemError ( )))
  149.             result = 0;
  150.     }
  151.  
  152.     return result;
  153. }
  154.  
  155. pascal OSErr AEGetDescData (const AEDesc *descP, DescType dType, void *dataP, Size max)
  156. {
  157.     OSErr err = noErr;
  158.  
  159.     if (!MoreAssert (descP && (dataP || !max)))
  160.         err = paramErr;
  161.     else if (!MoreAssert (descP->descriptorType == dType))
  162.         err = paramErr;
  163.     else if (dataP && max)
  164.     {
  165.         Size actualSize = ::GetHandleSize (descP->dataHandle);
  166.         if (!(err = MemError ( )))
  167.         {
  168.             if (actualSize > max) actualSize = max;
  169.             ::BlockMoveData (*(descP->dataHandle),dataP,actualSize);
  170.         }
  171.     }
  172.  
  173.     return err;
  174. }
  175.  
  176. #endif
  177.